// leaderPerson/pages/facePayDetail/facePayDetail.js
var common = require("../../../common/common.js").common;

var app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        checkedBalance: true,
        //余额
        checkedWe: false,
        //微信支付
        myInfo: {},
        // blanceSwitch: 1,
        balanceShow: false,
        weShow: false,
        disabled: false,
        form: {
            payment_channel: 1
        }
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        // if(!wx.getStorageSync('user').nickname){
        //   wx.showModal({
        //     title:'提示',
        //     content:'请先授权登录再进行该操作',
        //     success: function(res){
        //         wx.switchTab({
        //           url: '/pages/personal/personal',
        //         })
        //     }
        //   })
        //   return;
        // }
        if (app.globalData.user) {
            //如果直接拿到openid
            that.setData({
                uid: app.globalData.user.uid
            });
            wx.setStorageSync("user", app.globalData.user);
            // that.requestStart();
                } else {
            //回调拿到openid
            app.userInfoReadyCallback = (res => {
                that.setData({
                    uid: res.uid
                });
                wx.setStorageSync("user", res);
                // that.requestStart();
                        });
        }
        this.getBase();
        this.getBlanceSwitch();
        this.getUserInfo();
        if (options.scene) {
            var scene = decodeURIComponent(options.scene);
            console.log(scene);
            let arr = scene.split(":");
            let leader_id = arr[0].split("=");
            let money = arr[1].split("=");
            this.setData({
                "form.leader_id": leader_id[1],
                "form.money": money[1]
            });
            //设置过金额，则无法修改
                        if (Number(money[1]) && Number(money[1] != 0)) {
                this.setData({
                    disabled: true
                });
            }
        }
    },
    changePrice(e) {
        console.log(e);
        this.clearNoNum(e.detail.value);
    },
    //余额开关
    getBlanceSwitch() {
        let that = this;
        common.request({
            endpoint: "Balance/balance/balance_switch",
            method: "post",
            data: {},
            success: function(res) {
                if (res.code == 0) {
                    that.setData({
                        blanceSwitch: res.extra
                    });
                    // this.blanceSwitch = res.extra//1开启，0关闭
                                } else {
                    wx.showModal({
                        title: "提示",
                        content: res.message
                    });
                }
            },
            fail: function(res) {
                wx.showModal({
                    title: "提示",
                    content: res.message
                });
            }
        });
    },
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                wx.setNavigationBarColor({
                    frontColor: "#ffffff",
                    backgroundColor: res.extra.color_main
                });
                that.setData({
                    color_main: res.extra.color_main,
                    color_minor: res.extra.color_minor,
                    on_off_trans_free: res.extra.on_off_trans_free,
                    trans_for_free_barrier: res.extra.trans_for_free_barrier,
                    new_member_audit: res.extra.new_member_audit,
                    userAuditStatus: res.extra.userAuditStatus ? res.extra.userAuditStatus : 2,
                    face_pay_set: res.extra.face_pay_set
                });
                let leader_pay_type = that.data.face_pay_set.leader_pay_type.split(",");
                leader_pay_type.forEach((item, index) => {
                    console.log(item);
                    if (item == 1) {
                        that.setData({
                            weShow: true,
                            "form.payment_channel": 2
                        });
                        console.log(2222);
                    }
                    if (item == 2) {
                        that.setData({
                            balanceShow: true,
                            "form.payment_channel": 1
                        });
                        console.log(3333);
                    }
                });
            }
        });
    },
    getUserInfo() {
        let that = this;
        common.request({
            endpoint: "My/myinfo/myinfo",
            method: "post",
            data: {
                openid: wx.getStorageSync("user").openid
            },
            success: function(res) {
                if (res.code == 0) {
                    that.setData({
                        myInfo: res.extra
                    });
                } else {
                    wx.showToast({
                        title: res.message
                    });
                }
            }
        });
    },
    //验证两位小数
    clearNoNum(num) {
        //先把非数字的都替换掉，除了数字和.
        let newNum = num.replace(/[^\d.]/g, "");
        //保证只有出现一个.而没有多个.
                let newNum2 = newNum.replace(/\.{2,}/g, ".");
        //必须保证第一个为数字而不是.
                let newNum3 = newNum2.replace(/^\./g, "");
        //保证.只出现一次，而不能出现两次以上
                let newNum4 = newNum3.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
        //只能输入两个小数
                let newNum5 = newNum4.replace(/^(\-)*(\d+)\.(\d\d).*$/, "$1$2.$3");
        this.setData({
            "form.money": newNum5
        });
    },
    changeTextarea(e) {
        console.log(e);
        this.setData({
            "form.remark": e.detail.value
        });
    },
    pay() {
        let that = this;
        common.request({
            endpoint: "Leader/leaderfacepay/create-order",
            method: "post",
            data: that.data.form,
            success: res => {
                //余额
                if (that.data.form.payment_channel == 1) {
                    wx.showLoading({
                        title: "支付处理中",
                        mask: true,
                        success: function(rescc) {
                            common.request({
                                endpoint: "Leader/leaderfacepay/sync-notify",
                                method: "post",
                                data: {
                                    order_sn: res.extra.order_sn
                                },
                                success: resccc => {
                                    wx.hideLoading({});
                                    wx.showToast({
                                        title: "支付成功"
                                    });
                                    setTimeout(function() {
                                        wx.reLaunch({
                                            url: "/leaderPerson/pages/facePayOrderList/facePayOrderList"
                                        });
                                    }, 500);
                                },
                                fail(err) {
                                    wx.showModal({
                                        title: "提示",
                                        content: err.message
                                    });
                                }
                            });
                        }
                    });
                } else {
                    wx.requestPayment({
                        timeStamp: res.extra.pay_info.timeStamp,
                        nonceStr: res.extra.pay_info.nonceStr,
                        package: res.extra.pay_info.package,
                        signType: res.extra.pay_info.signType,
                        paySign: res.extra.pay_info.paySign,
                        success: function(resc) {
                            console.log(resc);
                            wx.showLoading({
                                title: "支付处理中",
                                mask: true,
                                success: function(rescc) {
                                    common.request({
                                        endpoint: "Leader/leaderfacepay/sync-notify",
                                        method: "post",
                                        data: {
                                            order_sn: res.extra.order_sn
                                        },
                                        success: resccc => {
                                            wx.hideLoading({});
                                            wx.showToast({
                                                title: "支付成功"
                                            });
                                            setTimeout(function() {
                                                wx.reLaunch({
                                                    url: "/leaderPerson/pages/facePayOrderList/facePayOrderList"
                                                });
                                            }, 500);
                                        },
                                        fail(err) {
                                            wx.showModal({
                                                title: "提示",
                                                content: err.message
                                            });
                                        }
                                    });
                                }
                            });
                        },
                        fail: function(res) {
                            wx.showModal({
                                title: "提示",
                                content: res.message
                            });
                        }
                    });
                }
            },
            fail(err) {
                wx.showModal({
                    title: "提示",
                    content: err.message
                });
            }
        });
    },
    radioChange(e) {
        // console.log(e);
        this.setData({
            "form.payment_channel": e.detail.value
        });
        if (e.detail.value == 1) {
            this.setData({
                checkedBalance: true,
                checkedWe: false,
                "form.payment_channel": 1
            });
        }
        if (e.detail.value == 2) {
            this.setData({
                checkedBalance: false,
                checkedWe: true,
                "form.payment_channel": 2
            });
        }
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {}
});